<template>
  <div style="height:100%;position:relative;">
    <div :id="id" style="height: 100%" :style="{visibility:data&&data.length>0?'visible':'hidden'}"></div>
    <div v-show="!data||data.length===0" class="no-data">暂无数据</div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { hexToRgba } from "../../../util/tools"

export default {
  name: "bar-special",
  props: {
    id: {
      type: String,
      default: "bar-special",
    },
    color: {
      type: Array,
      default: () => [],
    },
    data: {
      type: Array,
      default: () => [],
    },
    isSeparate: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    data: {
      deep: true,
      handler(nVal) {
        if (this.chart) {
          this.setOption();
        } else {
          this.initChart();
        }
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id), "macarons");
      this.setOption();

      this.chart.on("click", (param) => {
        this.$emit("onClick", this.data[param.dataIndex].key);
      });

      window.addEventListener("resize", () => {
        this.chart && this.chart.resize();
      });
    },
    setOption() {
      if (!this.data || Object.keys(this.data).length === 0) {
        return;
      }

      let xData = this.data.map((item) => {
        return item.name;
      });
      let yData = this.data.map((item) => {
        return item.count;
      });

      let option = {
        tooltip: {
          trigger: "axis",
          borderWidth: 0,
          backgroundColor: "rgba(50, 50, 50, 0.7)",
          textStyle: { color: "#ffffff" },
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: 25,
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: true,
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            color: "#689FF6",
            interval: 0,
          },
          splitLine: { show: false },
          data: xData,
        },
        yAxis: {
          offset: 0,
          type: "value",
          splitNumber: 3,
          minInterval: 1,
          axisLine: { show: false },
          axisTick: { show: false },
          axisLabel: {
            color: "#689FF6",
          },
          splitLine: {
            lineStyle: {
              color: "rgba(235,236,240,0.1)",
            },
          },
        },
        series: [
          {
            name: 'hill',
            type: "pictorialBar",
            barWidth: "60%",
						// symbol: 'path://M150 50 L130 130 L170 130 Z',
						// symbol: 'path://M0,10 L10,10 C6.5,10 6.5,6 6,0 C5.5,5 5.5,10 0,10 z',
						// symbol: 'path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z',
						symbol: 'path://M0,10 L10,10 C7.5,10 5.5,5 5,0 C4.5,5 2.5,10 0,10 z',
            label: {
              show: true,
              position: "top",
              color: "#fff",
							fontSize: 10
            },
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
								{ offset: 0, color: this.color[0] },
                { offset: 1, color: this.color[1] },
              ]),
            },
            data: yData,
          },
        ],
      };

      this.chart.setOption(option);
    },
  },
};
</script>

<style lang="less" scoped>
.no-data {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c5c8ce;
}
</style>